<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Validate;
use app\common\model\Admin as AdminModel;

/**
 * 管理员管理
 * @package app\admin\controller
 */
class Admin extends AdminBase
{
    private $model = null; //模型实例

    # 构造方法初始化模型
    public function _initialize()
    {
        if (is_null($this->model)) {
            $this->model = new AdminModel;
        }
    }
    # 个人资料首页
    public function index()
    {
        $user=$this->model->getDataByWhere(false);
        $this->assign('data', $user);
        return $this->fetch();
    }
    # 修改信息
    public function info()
    {
        if (!request()->isAjax()) {
            $this->redirect('admin/admin/index');
        }
        $data=input('post.');
        $validate=[
            'rule'=>[
                'username'          => 'require|min:6|max:20',
                'email'             => 'require|email',
                'nickname'          => 'max:20',
            ],
            'message'=>[
                'username.require'  => '用户名必须',
                'username.min'      => '用户名不能少过6字符',
                'username.max'      => '用户名不能超过20字符',
                'email.require'     => '邮箱必须',
                'email'             => '邮箱格式有误',
                'nickname.max'      => '昵称不能超过20字符',
            ]
        ];
        $validate = new Validate($validate['rule'], $validate['message']);
        if (!$validate->check($data)) {
            if (!is_array($validate->getError())) {
                return json(['errno'=>1,'msg'=>$validate->getError()]);
            }
            $msg=[];
            foreach ($validate->getError() as $val) {
                $msg[]=$val;
            }
            return json(['errno'=>1,'msg'=>$msg]);
        }
        // dump($data);
        if (!$this->model->updateData($data)) {
            return json(['errno'=>1,'msg'=>'信息修改时发生错误']);
        }
        return json(['errno'=>0,'msg'=>'信息修改完成']);
    }
    # 修改密码
    public function password()
    {
        if (!request()->isAjax()) {
            $this->redirect('admin/admin/index');
        }
        $data=input('post.');
        $validate=[
            'rule'=>[
                'oldpassword'       => 'require',
                'password'          => 'require|min:6|max:15|alphaDash',
                'repassword'        => 'require|confirm:password',
            ],
            'message'=>[
                'oldpassword.require'   => '原密码必须',
                'password.require'      => '密码必须',
                'password.min'          => '密码不能少于6字符',
                'password.max'          => '密码不能超过15字符',
                'password.alphaDash'    => '密码只能是字母和数字，下划线_及破折号-',
                'repassword.require'    => '确认密码必须',
                'repassword.confirm'    => '两次密码输入不同'
            ]
        ];
        $validate = new Validate($validate['rule'], $validate['message']);
        if (!$validate->check($data)) {
            if (!is_array($validate->getError())) {
                return json(['errno'=>1,'msg'=>$validate->getError()]);
            }
            $msg=[];
            foreach ($validate->getError() as $val) {
                $msg[]=$val;
            }
            return json(['errno'=>1,'msg'=>$msg]);
        }
        
        if (!$user = $this->model->checkUser(session('admin.username'), $data['oldpassword'])) {
            return json(['errno'=>1,'msg'=>'原密码错误']);
        }
        if (!$this->model->updateData(passwdSet($data['password']), ['name'=>'password'])) {
            return json(['errno'=>1,'msg'=>'密码修改时发生错误']);
        }
        return json(['errno'=>0,'msg'=>'密码修改完成，下次登陆将使用新密码登陆']);
    }
}
